package com.ler.checkdemo.controller;

import com.ler.checkdemo.domain.User;
import com.ler.jcheckspringbootstarter.annation.Check;
import com.ler.jcheckspringbootstarter.config.HttpResult;
import com.ler.jcheckspringbootstarter.util.Checks;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lww
 * @date 2019-09-03 17:26
 */
@Api("检查测试")
@RestController
public class ExampleController {

	@ApiOperation("普通参数检查")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", value = "姓名"),
			@ApiImplicitParam(name = "age", value = "年龄"),
	})
	@GetMapping("/param")
	@Check(ex = "name != null", msg = "name为空！")
	@Check(ex = "age != null", msg = "age为空！")
	public String input(String name, Integer age) {
		Checks.isTrue(false, "Checks检查异常，类似Assert断言");
		return "Success";
	}

	@ApiOperation("对象参数检查")
	@PostMapping("/body")
	@Check(ex = "user.name != null", msg = "name为空！")
	@Check(ex = "user.age != null", msg = "age为空！")
	public HttpResult b(@RequestBody User user, HttpServletRequest request) {
		Assert.isTrue(false, "断言异常灰度不报！");
		return HttpResult.success();
	}

}
